<?php
/**
 * Created by openXtiger.org.
 * User: xtiger
 * Date: 2009-6-20
 * Time: 11:43:44
 */
function cms_sitemodel_select($site, $parentid = 0, $name = 'siteid',
        $id ='', $siteid = 0, $alt='', $property = '', $type = 0, $optgroup = 0) {
    if(!$id) $id = $name;
    $tree = xt_load('library','tree',TRUE);
    $optgroup_str = '';
    if($optgroup) $optgroup_str = "<optgroup label='\$name'></optgroup>";
    $data = "<select name='$name' id='$id' $property>\n";
    if($alt){
        $data.="<option value='0'>$alt</option>";
    }
    if(is_array($site)) {
        $sites = array();
        foreach($site as $id=>$s) {
            if($s['type']==1 || $s['type']==2) continue;
            $sites[$id] = array('id'=>$id, 'parentid'=>$s['parentid'], 'name'=>$s['sitename']);
        }
        $tree->tree($sites);
        $data .= $tree->get_tree($parentid, "<option value='\$id'\$selected>\$spacer\$name</option>\n", $siteid, '' , $optgroup_str);
    }
    $data .= '</select>';
    return $data;
}

function cms_sitemodel_listinfo($parentid = -1) {
    $db = xt_load('db','icweb');
    $sites = array();
    $where = $parentid > -1 ? " WHERE parentid='$parentid'" : '';
    $result = $db->query("SELECT * FROM `".xt_tname('cmssite','icweb')."`   $where ORDER BY `listorder`,`siteid`");
    while($r = $db->fetch_array($result)) {
        $sites[$r['siteid']] = $r;
    }
    $db->free_result($result);
    return $sites;
}

function  cms_sitemodel_get($siteid) {
    $db = xt_load('db','icweb');
    $data = $db->fetch_first("SELECT * FROM `".xt_tname('cmssite','icweb')."` WHERE `siteid`=$siteid");
    if(!$data) return false;
    if($data['setting'])
        eval("\$data['setting'] = $data[setting];");
    return $data;
}
function  cms_sitemodel_add($data,$setting) {
     $db = xt_load('db','icweb');
     $data['setting'] =  var_export(xt_stripslashes($setting), TRUE);  
     $siteid = $db->insert('cmssite',$data,1,'icweb');
     $cachemodel = xt_load('model','cms.cachemodel');
     $cachemodel('reflashCommon');
     return $siteid;    
}
function  cms_sitemodel_edit($siteid, $site, $setting, $pid) {
     $db = xt_load('db','icweb');
     $site['setting'] =  var_export(xt_stripslashes($setting), TRUE);
     $r = $db->update('cmssite', $site ,"`siteid`=$siteid", 'icweb');
     $cachemodel = xt_load('model','cms.cachemodel');
     $cachemodel('reflashCommon');
     return $r;
}
?>